﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace first_project
{
    class Asteroid
    {
        private const int MAX_LIFE = 5;
        private const int INITIAL_SPEED = 1;
        /// <summary>
        /// Game Window
        /// </summary>
        private GameWindow window;

        /// <summary>
        /// Asteroid image.
        /// </summary>
        private Texture2D image;

        /// <summary>
        /// Asteroid position.
        /// </summary>
        private Vector2 position;
        /// <summary>
        /// Getter and Setter for asteroid position.
        /// </summary>
        public Vector2 Position
        {
            get
            {
                return this.position;
            }
            set
            {
                this.position = value;
            }
        }

        /// <summary>
        /// Getter for asteroid collision rectangle.
        /// </summary>
        public Rectangle CollisionRectangle
        {
            get
            {
                return new Rectangle((int)position.X, (int)position.Y, (int)(image.Width * scale), (int)(image.Height * scale));
            }
        }

        /// <summary>
        /// Asteroid direction.
        /// </summary>
        private Vector2 direction;
        /// <summary>
        /// Getter and Setter for asteroid direction.
        /// </summary>
        public Vector2 Direction
        {
            get
            {
                return this.direction;
            }
            set
            {
                this.direction = value;
            }
        }
        /// <summary>
        /// Asteroid speed.
        /// </summary>
        private Vector2 speed;
        /// <summary>
        /// Asteroid scale.
        /// </summary>
        private float scale;
        /// <summary>
        /// Asteroid life.
        /// </summary>
        private int life;
        /// <summary>
        /// Getter and Setter for asteroid life.
        /// </summary>
        public int Life
        {
            get
            {
                return this.life;
            }
            set
            {
                this.life = value;
            }
        }

        public Asteroid(GameWindow window, Texture2D image, int life)
        {
            this.window = window;
            this.image = image;
            this.position = randomOffScreenPosition();
            this.direction = randomDirection();
            if (life > MAX_LIFE)
                this.life = MAX_LIFE;
            else
                this.life = life;
            this.scale = (float)this.life / (float)MAX_LIFE;
            this.speed = new Vector2(INITIAL_SPEED / scale, INITIAL_SPEED / scale); ;
        }

        public Asteroid(GameWindow window, Texture2D image, Vector2 position,int life)
        {
            this.window = window;
            this.image = image;
            this.position = position;
            this.direction = randomDirection();
            if (life > MAX_LIFE)
                this.life = MAX_LIFE;
            else
                this.life = life;
            this.scale = (float)this.life / (float)MAX_LIFE;
            this.speed = new Vector2(INITIAL_SPEED / scale, INITIAL_SPEED / scale); ;
        }


        public void Update(GameTime gameTime)
        {
            moveAsteroid();
        }


        public void Draw(SpriteBatch spriteBatch)
        {

            spriteBatch.Draw(image, position, null, Color.White, 0, new Vector2(image.Width / 2, image.Height / 2), scale, SpriteEffects.None, 0);

        }

        private Vector2 randomOffScreenPosition()
        {
            Random randomNum = new Random();
            Vector2 randomPosition = Vector2.Zero;
            int randomSide = randomNum.Next(0, 3);
            float randomX = randomNum.Next(0, window.ClientBounds.Width);
            float randomY = randomNum.Next(0, window.ClientBounds.Height);

            switch (randomSide)
            {
                case 0:
                    randomPosition = new Vector2(0, randomY);
                    break;
                case 1:
                    randomPosition = new Vector2(window.ClientBounds.Width, randomY);
                    break;
                case 2:
                    randomPosition = new Vector2(randomX, 0);
                    break;
                case 3:
                    randomPosition = new Vector2(randomX, window.ClientBounds.Height);
                    break;
            }

            return randomPosition;
        }

        private Vector2 randomDirection()
        {
            Random randomNum = new Random();
            float rotation = (float)randomNum.Next(0, 359);

            return new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));
        }

        // Método responsável pelo moviento do asteróide.
        private void moveAsteroid()
        {
            position += direction * speed;

            if (position.X >= window.ClientBounds.Width + image.Width / 2)
            {
                position.X = -image.Width / 2;
            }
            else if (position.X <= -image.Width / 2)
            {
                position.X = window.ClientBounds.Width + image.Width / 2;
            }
            else if (position.Y >= window.ClientBounds.Height + image.Height / 2)
            {
                position.Y = -image.Height / 2;
            }
            else if (position.Y <= -image.Height / 2)
            {
                position.Y = window.ClientBounds.Height + image.Height / 2;
            }
        }

        public bool processShotColision()
        {
            this.life -= 1;
            if (this.life == 0)
                return true;
            this.scale = (float)this.life / (float)MAX_LIFE;
            this.speed = new Vector2(INITIAL_SPEED / scale, INITIAL_SPEED / scale);

            return false;
        }
    }
}